<?php

namespace App\Http\Controllers\App;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\ArticleDetail;
use App\Models\ArticleCategory;
use App\Models\ArticleLabel;
use App\Models\ArticleLabelRelation;
use App\Http\Controllers\App\BaseController;

class ArticleController extends BaseController
{   

    /**
     * 获取文章列表
     *
     * @param Request $request
     * @return void
     */
    public function list(Request $request)
    {
        $cate_id = $request->input('cate_id', 0);
        $type    = $request->input('type', 0);
        $status  = $request->input('status', 0);
        $limit   = $request->input('limit', 20);

        $query = new ArticleDetail;

        if($cate_id){

            $query->where('cate_id', $cate_id);

        }

        if($type){

            $query->where('type', $type);

        }

        if($status){

            $query->where('status', $status);

        }

        $info = $query->orderBy('updated_at', 'desc')->paginate($limit);

        return self::json($info);

    }

    /**
     * 获取文章详情
     *
     * @param Request $request
     * @return void
     */
    public function detail(Request $request)
    {
        $id = $request->input('id', 0);

        $info = ArticleDetail::find($id);

        return self::json($info);
    }

    /**
     * 添加修改文章详情
     *
     * @param Request $request
     * @return void
     */
    public function add_detail(Request $request)
    {
        $id       = $request->input('id', 0);
        $title    = $request->input('title', '');
        $cover    = $request->input('cover', '');
        $cate_id  = $request->input('cate_id', 0);
        $intro    = $request->input('intro', '');
        $content  = $request->input('content', '');
        $type     = $request->input('type', 1);
        $is_link  = $request->input('is_link', 1);
        $status   = $request->input('status', 1);
        $admin_id = session('admin_id', 0);

        if($id){

            $info = ArticleDetail::find($id);

            if($title)   $info->title = $title;
            
            if($cover)   $info->cover = $cover;
            
            if($cate_id) $info->cate_id = $cate_id;
            
            if($intro)   $info->intro = $intro;
            
            if($content) $info->content = $content;
            
            if($type)    $info->type = $type;

            if($is_link) $info->is_link = $is_link;
            
            if($status)  $info->status = $status;

            $info->admin_id = $admin_id;
            $info->save();

            return self::success('修改成功');

        }else{

            $info = new ArticleDetail;

            $info->title    = $title;
            $info->cover    = $cover;
            $info->cate_id  = $cate_id;
            $info->intro    = $intro;
            $info->content  = $content;
            $info->type     = $type;
            $info->status   = $status;
            $info->is_link  = $is_link;
            $info->admin_id = $admin_id;
            $info->save();

            return self::success('添加成功');

        }

    }

    /**
     * 删除文章
     *
     * @param Request $request
     * @return void
     */
    public function del_detail( Request $request )
    {
        $id = $request->input('id', 0);

        if($id){

            ArticleDetail::destroy($id);

            return self::success('删除成功');

        }

    }

    /**
     * 获取分类列表
     *
     * @param Request $request
     * @return void
     */
    public function cate_list(Request $request)
    {
        $limit   = $request->input('limit', 20);

        $info = ArticleCategory::where('parent_id', 0)->where('status', 1)->orderBy('updated_at', 'desc')->get();

        return self::json($info);
        
    }

    /**
     * 获取分类详情
     *
     * @param Request $request
     * @return void
     */
    public function cate_detail(Request $request)
    {
        $id = $request->input('id', 0);

        $info = ArticleCategory::find($id);

        return self::json($info);
    }

    /**
     * 添加修改分类
     *
     * @param Request $request
     * @return void
     */
    public function add_cate( Request $request )
    {
        $id        = $request->input('id', 0);
        $title     = $request->input('title', '');
        $cover     = $request->input('cover', '');
        $parent_id = $request->input('parent_id', 0);
        $status    = $request->input('status', 0);
        $admin_id  = session('admin_id', 0);

        if($id){

            $cate = ArticleCategory::find($id);

            if($title){

                $cate->title = $title;

            }

            if($cover){

                $cate->cover = $cover;

            }

            if($parent_id){

                $cate->parent_id = $parent_id;

            }

            if($status){

                $cate->status = $status;

            }

            $cate->admin_id = $admin_id;
            $cate->save();

            return self::success('修改成功');

        }else{

            $cate = new ArticleCategory;

            $cate->title = $title;
            $cate->cover = $cover;
            $cate->parent_id = $parent_id;
            $cate->status = 1;
            $cate->admin_id = $admin_id;
            $cate->save();

            return self::success('添加成功');

        }

    }

    /**
     * 添加修改标签
     *
     * @param Request $request
     * @return void
     */
    public function add_label( Request $request )
    {
        $id        = $request->input('id', 0);
        $title     = $request->input('title', '');
        $status    = $request->input('status', 0);
        $admin_id  = session('admin_id', 0);

        if($id){

            $cate = ArticleLabel::find($id);

            if($title){

                $cate->title = $title;

            }

            if($status){

                $cate->status = $status;

            }

            $cate->admin_id = $admin_id;
            $cate->save();

            return self::success('修改成功');

        }else{

            $cate = new ArticleLabel;

            $cate->title = $title;
            $cate->status = 1;
            $cate->admin_id = $admin_id;
            $cate->save();

            return self::success('添加成功');

        }

    }

    /**
     * 删除标签
     *
     * @param Request $request
     * @return void
     */
    public function del_label( Request $request )
    {
        $id        = $request->input('id', 0);

        if($id){

            $sum = ArticleLabelRelation::where('label_id', $id)->count();

            if($sum > 0) return self::error('当前标签下有文章，不可删除');

            ArticleLabel::destroy($id);

            return self::success('删除成功');

        }

    }

    /**
     * 删除分类
     *
     * @param Request $request
     * @return void
     */
    public function del_cate( Request $request )
    {
        $id        = $request->input('id', 0);

        if($id){

            $num = ArticleCategory::where('parent_id', $id)->count();

            if($num > 0) return self::error('当前分类下有子分类，不可删除');

            $sum = ArticleDetail::where('cate_id', $id)->count();

            if($sum > 0) return self::error('当前分类下有文章，不可删除');

            ArticleCategory::destroy($id);

            return self::success('删除成功');

        }

    }

}
